//
//  UIView+Ex.swift
//  SHCountDown
//
//  Created by jiaxing liu on 2019/1/25.
//  Copyright © 2019 jiaxing liu. All rights reserved.
//

import UIKit

extension UIView {

    // MARK - x
    var x: CGFloat {
        get {
            return frame.origin.x
        }
        set(newValue) {
            var temFrame: CGRect    = frame
            temFrame.origin.x       = newValue
            frame                   = temFrame
            
        }
    }
    // MARK - y
    var y: CGFloat {
        get {
            return frame.origin.y
        }
        set(newValue) {
            var temFrame: CGRect    = frame
            temFrame.origin.y       = newValue
            frame                   = temFrame
        }
    }
    
    // MARK - width
    var width: CGFloat {
        get {
            return frame.size.width
        }
        set(newValue) {
            var temFrame: CGRect    = frame
            temFrame.size.width       = newValue
            frame                   = temFrame
        }
    }
    
    // MARK - height
    var height: CGFloat {
        get {
            return frame.size.height
        }
        set(newValue) {
            var temFrame: CGRect    = frame
            temFrame.size.width       = newValue
            frame                   = temFrame
        }
    }
    
    // MARK - size
    var size: CGSize {
        get {
            return frame.size
        }
        set(newValue) {
            var temFrame: CGRect    = frame
            temFrame.size           = newValue
            frame                   = temFrame
        }
    }
    
    // MARK - centerX
    var centerX: CGFloat {
        get {
            return center.x
        }
        set(newValue) {
            var temCenter: CGPoint    = center
            temCenter.x               = newValue
            center                    = temCenter
        }
    }
    
    // MARK - centerY
    var centerY: CGFloat {
        get {
            return center.y
        }
        set(newValue) {
            var temCenter: CGPoint    = center
            temCenter.y               = newValue
            center                    = temCenter
        }
    }
    
    // MARK - maxX
    var maxX: CGFloat {
        get {
            return frame.maxX
        }
    }
    
    // MARK - maxY
    var maxY: CGFloat {
        get {
            return frame.maxY
        }
    }
    
    // MARK - minY
    var minY : CGFloat {
        get {
            return frame.minY
        }
    }
    
    // MARK - bottom
    var bottom : CGFloat {
        get {
            return frame.size.height + frame.origin.y
        }
    }
    
    // MARK - right
    var right : CGFloat {
        get {
            return frame.size.width + frame.origin.x
        }
    }
    
    func mediateX(_ subViewWidth: CGFloat) -> CGFloat {
        return ((self.width - subViewWidth)*0.5)
    }
    
    func mediateY(_ subViewHeight: CGFloat) -> CGFloat {
        return ((self.height - subViewHeight) * 0.5)
    }
    
    // MARK - 设置圆角
    func setViewBorder(cornerRadius: CGFloat = 3 ,color : UIColor)  {
        layer.masksToBounds = true
        layer.cornerRadius = cornerRadius
        layer.borderColor = color.cgColor
        layer.borderWidth = 1
        layer.shouldRasterize = true
        layer.rasterizationScale = layer.contentsScale
    }
    
    func setViewBorderWidth(_ borderWidth: CGFloat = 1 , _ borderColor : UIColor) {
        layer.borderWidth = borderWidth
        layer.borderColor = borderColor.cgColor
    }
    
    /// 获取view所在的试图控制器
    ///
    /// - Returns: UIViewConroller or nil
    func selfController() -> UIViewController? {
        var next: UIView? = self
        repeat {
            if let nextResponder = next?.next {
                if nextResponder.isKind(of: UIViewController.self){
                    return (nextResponder as! UIViewController)
                }
            }
            next = next?.superview
        }while next != nil
        return nil
    }
}
